//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.server;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

import tv.dyndns.kishibe.client.game.GameMode;
import tv.dyndns.kishibe.client.game.ProblemGenre;
import tv.dyndns.kishibe.client.game.ProblemType;
import tv.dyndns.kishibe.client.game.Transition;
import tv.dyndns.kishibe.client.packet.PacketRoomKey;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gwt.dev.util.Preconditions;
import com.google.inject.Inject;

public class GameManager {
	private static final Logger logger = Logger.getLogger(GameManager.class.toString());
	private final Game.Factory gameFactory;
	private final Map<Integer, Game> sessions = Maps.newConcurrentMap();
	private final Map<PacketRoomKey, Game> matchingSessions = Maps.newHashMap();
	private volatile List<PacketRoomKey> publicMatchingEventRooms = Lists.newArrayList();
	private volatile Game gameWhole = null;
	private final AtomicInteger sessionId = new AtomicInteger();

	@Inject
	public GameManager(Game.Factory gameFactory) {
		this.gameFactory = Preconditions.checkNotNull(gameFactory);
	}

	/**
	 * マッチング状態にあるセッションを返すか、なければ新たに作成する
	 * 
	 * @param gameMode
	 * @param roomName
	 * @param classLevel
	 * @param theme
	 * @param genres
	 * @param types
	 * @param publicEvent
	 * @param serverStatusManager
	 * @param userCode
	 * @param remoteAddress
	 * @param badUserManager
	 * @return
	 */
	public synchronized Game getOrCreateMatchingSession(GameMode gameMode, String roomName,
			int classLevel, String theme, Set<ProblemGenre> genres, Set<ProblemType> types,
			boolean publicEvent, ServerStatusManager serverStatusManager, int userCode,
			String remoteAddress, BadUserManager badUserManager) {
		if (badUserManager.isLimitedUser(userCode, remoteAddress)) {
			badUserManager.addLimitedUser(userCode, remoteAddress);
			// 隔離部屋は使用しない。通常のプレイヤーよりランクを下げることで対処する。
			// http://kishibe.dyndns.tv/qmaclone/wiki/wiki.cgi?page=BugTrack-QMAClone%2F490
			// gameMode = GameMode.limited;
		}

		PacketRoomKey roomKey = new PacketRoomKey(gameMode, gameMode == GameMode.theme ? theme
				: roomName, genres, types);

		Game result = matchingSessions.get(roomKey);

		// ゲームセッションが見つからない場合やマッチング中でない場合は新たにゲームセッションを作成する
		if (result == null || result.getTransition() != Transition.Matching) {
			boolean alone;
			boolean event;

			switch (gameMode) {
			case vsCom:
				alone = true;
				event = false;
				break;
			case whole:
				alone = false;
				event = false;
				break;
			case event:
				alone = false;
				event = true;
				break;
			case theme:
				alone = false;
				event = false;
				break;
			case limited:
				alone = false;
				event = false;
				break;
			default:
				alone = false;
				event = false;
				break;
			}

			int sessionId = this.sessionId.incrementAndGet();
			Game game = gameFactory.create(sessionId, classLevel, event, alone, theme, publicEvent);
			matchingSessions.put(roomKey, game);
			sessions.put(sessionId, game);
			updateEventRooms();
			result = game;
		}

		if (result == null) {
			String message = "ゲームセッションの作成または検索に失敗しました ";
			message += Objects.toStringHelper(this).add("gameMode", gameMode)
					.add("roomName", roomName).add("classLevel", classLevel).add("theme", theme)
					.add("genres", genres).add("types", types).add("publicEvent", publicEvent)
					.add("serverStatusManager", serverStatusManager).add("userCode", userCode)
					.add("remoteAddress", remoteAddress).add("badUserManager", badUserManager)
					.toString();
			logger.log(Level.SEVERE, message);
		}

		if (gameMode == GameMode.whole) {
			gameWhole = result;
		}

		return result;
	}

	/**
	 * セッションを返す
	 * 
	 * @param sessionId
	 *            セッションID
	 * @return セッション
	 */
	public Game getSession(int sessionId) {
		Game game = sessions.get(sessionId);
		if (game == null) {
			logger.log(Level.SEVERE, "ゲームセッションが見つかりませんでした sessionId=" + sessionId + " sessions="
					+ sessions.toString());
		}
		return game;
	}

	// 終了したセッションを消去する
	public void removeSession(int sessionId) {
		sessions.remove(sessionId);
		logger.log(Level.INFO, "ゲームセッションを削除しました sessionId =" + sessionId);
	}

	public void notifyMatchingCompleted() {
		updateEventRooms();
	}

	public List<PacketRoomKey> getPublicMatchingEventRooms() {
		return publicMatchingEventRooms;
	}

	private synchronized void updateEventRooms() {
		List<PacketRoomKey> eventRooms = Lists.newArrayList();
		for (Entry<PacketRoomKey, Game> entry : matchingSessions.entrySet()) {
			Game game = entry.getValue();
			if (game.isEvent() && game.isPublicEvent()
					&& game.getTransition() == Transition.Matching) {
				PacketRoomKey key = entry.getKey();
				eventRooms.add(key);
			}
		}
		this.publicMatchingEventRooms = eventRooms;
	}

	public int getNumberOfPlayersInWhole() {
		Game game = gameWhole;

		if (game == null || game.getTransition() != Transition.Matching) {
			return 0;
		} else {
			return game.getNumberOfHumanPlayer();
		}
	}

	public int getNumberOfSessions() {
		return sessions.size();
	}

	public int getNumberOfPlayers() {
		int numberOfPlayers = 0;
		for (Game game : sessions.values()) {
			numberOfPlayers += game.getNumberOfHumanPlayer();
		}
		return numberOfPlayers;
	}
}
